<style lang="less">
@import "./login.less";
@import "../styles/common.less";
</style>

<template>
  <div class="login">
    <!--<div class="login-type" v-if="type != 'login'">-->
      <!--<div class="content">-->
        <!--<div class="left" @click="admin()">-->
          <!--<div class="div-icon">-->
            <!--<Icon type="ios-person" />-->
          <!--</div>-->
          <!--<div class="div-title">我是管理员</div>-->
        <!--</div>-->
        <!--<div class="left" @click="editor()">-->
          <!--<div class="div-icon">-->
            <!--<Icon type="ios-paper" />-->
          <!--</div>-->
          <!--<div class="div-title">我要编辑</div>-->
        <!--</div>-->
      <!--</div>-->
    <!--</div>-->
    <div class="login-con" v-if="type == 'login'" @keydown.enter="handleSubmit">
      <Card :bordered="false">
        <p slot="title">
          <Icon type="log-in"></Icon>欢迎登录
        </p>
        <div class="form-con">
          <Form ref="loginForm" :model="form" :rules="rules">
            <FormItem prop="userName">
              <Input v-model="form.userName" placeholder="请输入账号" id="username">
                <span slot="prepend">
                  <Icon :size="16" type="ios-person"></Icon>
                </span>
              </Input>
            </FormItem>
            <FormItem prop="password">
              <Input type="password" v-model="form.password" placeholder="请输入密码">
                <span slot="prepend">
                  <Icon :size="14" type="ios-lock"></Icon>
                </span>
              </Input>
            </FormItem>
            <FormItem>
              <Button @click="handleSubmit" type="primary" long>登录</Button>
              <!--<Button @click="userAdd"  long>添加</Button>-->
            </FormItem>
            <!--<div class="botton">-->
              <!--<span class="right" @click="resetPwd">找回密码</span>-->
            <!--</div>-->
          </Form>
        </div>
      </Card>
    </div>
    <div class="version-div">
      <a href="http://www.jnaw.top" target="_blank">
        ©2018 AllView CWBU All Rights Reserved.
        <span
          :title="'v'+version_backend"
          v-if="available == true"
          v-text="'v'+version"
        ></span>
        <span
          :title="'v'+version_backend+',服务器处于测试阶段，可能会出现不可预知的错误'"
          v-if="available == false"
          v-text="'v'+version"
        ></span>
      </a>
    </div>
  </div>
</template>
<script>
import Cookies from "js-cookie";
import Util from "../../libs/util";
import Utils from "../../libs/mod.utils";
import { setToken, getToken } from "@/libs/util";
import Modal from "../../libs/mod.Modal";
import baiduMap from "../../libs/mod.baiduMap";
export default {
  data() {
    return {
      version: Util.version,
      available_version_backend: Util.available_version_backend,
      version_backend: "",
      available: true,
      form: {
        userName: "",
        password: "",
        id: "",
        username: "",
        nim_token: ""
      },
      rules: {
        userName: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      },
      id: "",
      username: "",
      nim_token: "",
      status: "",
      ApplyListModal: false,
      ApplyListData: [],
      longitude: 0, // 定位信息
      latitude: 0,
      address: "",

      type: "login"
    };
  },
  created: function() {
    var _this = this;
    var title = Util.title(); // 页面标题
    var page_icon = Util.page_icon; // 页面ico
    window.document.title = title;
    // 设置ico
    var linkEle = document.getElementById("link_icon_id");
    linkEle.href = "/vendors/images/" + page_icon;
    linkEle.type = "";
    linkEle.type = "image/x-icon";

    _this.$nextTick(function() {
      // 定位
      baiduMap.getBaiduMapObj(function(_data) {
        _this.longitude = _data.longitude || 0;
        _this.latitude = _data.latitude || 0;
        _this.address = _data.address || "";
      });
    });
  },
  mounted() {
    var _this = this;
    var login_mobile = Utils.getCookie("login_mobile");
    var login_password = Utils.getCookie("login_password");
    if (login_mobile && login_password) {
      login_password = Utils.uncompile(login_password); //解密
      _this.form.userName = login_mobile;
      _this.form.password = login_password;
    }
    _this.get_version_backend();

    _this.$nextTick(function() {
      $("#username input").focus();
      _this.$Message.destroy();
      _this.$Message.destroy();
      var logout = sessionStorage.getItem("logout");
      if (logout == 1) {
        sessionStorage.removeItem("logout");
        window.location.reload(); // 刷新页面
      }
    });
    _this.checkLeave();
  },
  methods: {
    checkLeave: function() {
      var _this = this;
      window.onbeforeunload = function(e) {};
    },
    // 显示登录
    admin: function() {
      var _this = this;
      _this.type = "login";
    },
    // 富文本编辑
    editor: function() {
      var _this = this;
      _this.$router.push({
        name: "editor_page"
      });
    },
    //找回密码
    resetPwd: function() {
      var _this = this;
      var mobile = _this.form.userName;
      sessionStorage.setItem("resetPwdmobile", mobile);
      console.log("mobile", mobile);
      _this.$router.push({
        name: "resetPwd"
      });
    },
    // 后台版本号
    get_version_backend: function() {
      var _this = this;
      var data = {};
      var params = {
        url: "/version/index",
        type: Util.method.post,
        data: data,
        success: _successFun,
        error: _errorFun
      };
      Util.ajax(_this, params, true);
      function _successFun(res) {
        if (res.result == 0) {
          _this.version_backend = res.data;
          var available_version_backend = _this.available_version_backend;
          var version_backend = _this.version_backend;
          var version_backend_arr = version_backend.split(".");
          var available_version_backend_arr = available_version_backend.split(
            "."
          );
          _this.available = false;
          if (
            version_backend_arr.length > 2 &&
            version_backend_arr[0] == available_version_backend_arr[0] &&
            version_backend_arr[1] == available_version_backend_arr[1]
          ) {
            _this.available = true;
          }
        }
      }

      function _errorFun(err) {
        _this.$Message.destroy();
        _this.$Message.error("服务器内部错误，暂时无法使用!");
      }
    },
    // 加人
    // userAdd: function() {
    //   var _this = this;
    //   var data = {
    //     mobile: "15668334772",
    //     password: "1",
    //     name: "刘晨"
    //   };
    //   var params = {
    //     url: "/user/add",
    //     type: Util.method.post,
    //     data: data,
    //     success: _successFun,
    //     error: _errorFun
    //   };
    //   Util.ajax(_this, params, true);
    //   function _successFun(res) {
    //     console.log(success)
    //   }
    //
    //   function _errorFun(err) {
    //     console.log(success)
    //   }
    // },
    handleSubmit() {
      this.$refs.loginForm.validate(valid => {
        var _this = this;
        var version_backend = _this.version_backend;
        if (version_backend == "") {
          _this.$Message.destroy();
          _this.$Message.error("服务器内部错误，暂时无法使用!");
          return false;
        }
        if (_this.available == false) {
          _this.$Message.destroy();
          _this.$Message.error("服务器版本错误，暂时无法使用!");
          return false;
        }
        if (valid) {
          var data = {
            mobile: _this.form.userName,
            password: _this.form.password
          };
          var params = {
            url: "/user/login",
            type: Util.method.post,
            data: data,
            success: _successFun
          };
          Util.ajax(_this, params);
          function _successFun(res) {
            if (res.result == 0) {
              var data = res.data;
              localStorage.setItem("loginData", Util.toStr(data));
              var username = data.username;
              var id = data.id;
              var is_admin = data.is_admin;
              var need_reset_password = data.need_reset_password;
              _this.id = id;
              _this.username = username;
              var login_password = Utils.compile(_this.form.password); //加密
              Utils.delCookie("login_mobile");
              Utils.delCookie("login_password");
              Utils.setCookie("login_mobile", _this.form.userName, 30);
              Utils.setCookie("login_password", login_password, 30);

              // if (is_admin) {
              //   if (need_reset_password) {
              //     // 修改密码
              //     _this.$Message.destroy();
              //     _this.$Message.success("首次登录请先修改密码!");
              //     _this.resetPwd();
              //   } else {
              //     // 管理员
              //     _this.getUserInfoFun();
              //   }
              // } else {
              //   _this.$Message.destroy();
              //   _this.$Message.error("您没有管理员权限!");
              // }
              _this.getUserInfoFun();
            } else {
              _this.$Message.destroy();
              _this.$Message.error("用户名或密码错误!");
            }
          }
        }
      });
    },
    getUserInfoFun: function() {
      var _this = this;
      var data = {
        id: _this.id
      };
      var params = {
        url: "/user/query",
        type: Util.method.post,
        data: data,
        success: _successFun
      };
      Util.ajax(_this, params);
      function _successFun(res) {
        if (res.result == 0) {
          var _data = res.data || {};
          localStorage.setItem("userInfo", Util.toStr(_data));
          var name = _data.name;
          var avatar = _data.avatar;
          var thumb = avatar.thumb ? avatar.thumb : Util.constant.personImg;
          _this.$store.state.app.userAvator = thumb;
          _this.$store.state.app.userName = name;
          Util.sessionSet("token", _data.id);

          // 关闭所有，除了home
          var tagNaveListStr = localStorage.getItem("tagNaveList");
          if (tagNaveListStr != "") {
            var tagNaveListNew = [];
            var tagNaveList = Utils.toJson(tagNaveListStr);
            for (var k in tagNaveList) {
              if (!tagNaveList[k]) {
                continue;
              }
              var name = tagNaveList[k].name;
              if (name == "home") {
                tagNaveListNew.push(tagNaveList[k]);
                break;
              }
            }
            localStorage.setItem("tagNaveList", Utils.toStr(tagNaveListNew));
          }
          _this.$router.push({
            name: "_home"
          });
        } else {
          _this.$Message.destroy();
          _this.$Message.error("查询用户信息失败!");
        }
      }
    }
  }
};
</script>

<style>
</style>
